/**
 * Created by jiashuai.bao on 2019-06-17.
 */

package com.cykj.pos.payment;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.util.OrderUtil;
import com.sec.sdk.SecClient;
import com.sec.sdk.bean.BaseResponse;
import com.sec.sdk.bean.RemitBatchRequestDTO;
import com.sec.sdk.bean.RemitDetailRequestDTO;
import com.sec.sdk.constants.SecGatewayConstants;
import org.junit.Test;
import org.springframework.core.annotation.OrderUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 对应文档 5.1 银行卡打款申请 （渠道版商户适用，接口文档回标注标准版还是渠道版）
 **/

public class ChannelRemitTest2 {
    /**
     * 商户私钥 请根据文档描述生成并妥善保管！
     */

    @Test
    public void httpTest() throws Exception {
        /**
         * 商户私钥 请根据文档描述生成并妥善保管！
         * */
        String merPrivate = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC0EvfuIb9IOty4Whr0Rw7xOfsKw1r/VlleYeFX1hGDtzl/w3Q87JZVAj8uOp99hLkXUQJATSdHYdFpM2auMRrb2K8NSZt2jZortzFy63QRK11O26xFIT4gvjXy9gkUMG7p7FUw5wx0MEx0n6GfrQgp3P16rhdKA2vGS9p06U9XrI28wHEbo68iZIrxPbejbA/L3+CCjjXdrDJR3LU8G5TOy9GfUI/S77Af1/PmWma7vTksoPeLZ4MnNK32PVt6CoZBHe/ZVcfn6CEFc9j3Ps5TZPNzUKnn5Y3LqfGFqZq9h4QJRbnQ1cBu61wcLBIyln8dy/sOqxr7QUsNQT88prrzAgMBAAECggEBAIOrGv5UTrlR0PI7cFoNWy+nZZiJH5B0YwRQLaU8MbJN1w1ErUme9I1UBz6WPuhYk74ozayWvZHERIrXuszDuw/lcJoWqEYXZBNy8AVHhKbJPoDavywNEpB+hJQSsrDAeVkyJViMmN6cMktULanDq6RwoieCBCVqOvPTovi5X/qKRQiqkbuin69zaMfjwzN/N1E1qx4HQW3gqQ1kYvlcSBa1F+nhUzgQtj2KRn01he0lkn1FVtJwxu01uyzfsjAdqYSDY/23bFxX0Z3BjhPjh0wC+oyqC9JmQa0jrq43aI6kH+wPk7MhH0spUkwIvAmtzqCzXJK6EAnhPw2DSw8w+QECgYEA8nxXDtUfQyyoVTrLFynAfcx5gX+j4gGO/qhhRpbbchfSR91fhIIV8a0KRQDL73EFqHJ/lkfyLQgDi0ejOzHgeuSYvbTYufjtpkTNOurfszAEFfeaRN0CjJpNFc6ZMfi6G56aA6y0jS8gSEvDNAUs0td4z7dW8Ex/VQ5+ra6l8osCgYEAvhwsCszyih8spuhiU8PDyaPfFSP7VxSaN0MuaqPjC3MJW8bvb4dc9VVPwX2Nw/MK/d1N+feZyNdTHCwFvsFIw6K2Ym4xcKNPXiyBHaJ1Wv220ZqhUoNlgSHHCqGTv9YWEeoISHYTQTOcFExgRvUz3UKjh/bgavB7T2jSrX30bjkCgYEAj4TCbjf78eFLnG5Y59PBZ3OPcvMeB1VO0jDMql/9cFDjF7/Rmfb4j/sP+flXutdVNvbVxxe8IaT9nAEBfratm28TbCzOnXvVBLTyF24OitrfgDT1bM62hIuLFl4aU/ZYCKLdm1Er5+g9niyhU3fAqOX5Tk/AOCe+sZQLrDaTZ7MCgYEApI6OMOsmJhjF9dPBCPCzUJ7gQtRagw36T0t/qh8iMI7q0t5n8woglNu7paigRbTjFsuw62Y/X3HDJueUMYHtEiqN5LWtZep2kPXE7XnqS1DCJrzKVzoVaqlqOPGTisItaGRDtzU9hXq+53MVA1bMBgHgu5geuXJfymTZuAMGyUECgYAqjt9vybdfTGJybOqxX+qtnxp3O4faWWc2eNeF8iDkP0mFTD6QnPIlV4fgsMpEsW4cx9IglJAyhPL2mgTD5Wo3Fwbchf7n2T5htZdRDAh7JmZDEs20WutdPHpLVHO1rGXMuPLHduLx6MJlHL2bNRa2lr/vreYmQDoGzpy4Ow+RPg==";
        /**
         * 商户应用ID
         * */
        String appId = "10210204371901";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = df.format(new Date());
        String timestamp = str;
        SecClient secClient = new SecClient(SecGatewayConstants.SERVER_URL, "settle.remit.api.payment", appId, merPrivate, "001", timestamp, 20000, 20000);

        String orderAmt = "0.02";//打款金额(元)
        String recvCustName = "朱辉";//收款者姓名
        String recvMobile = "13214402588";//收款者手机号
        String recvIdType = "IDENTITY（居民身份证）";//收款者证件类型
        String recvIdNo = "222405198607161816";//收款证件号
        String recvBankName = "中国银行";//收款银行
        String recvCardNo = "6217580600002701257";//收款者银行卡号

        //业务参数构建
        RemitBatchRequestDTO remitBatchRequestDTO = new RemitBatchRequestDTO();
        remitBatchRequestDTO.setCustBatchNo(UUID.randomUUID().toString().replace("-", "")); //商户批次号
        remitBatchRequestDTO.setBatchNum(1);//批次总笔数
        //new BigDecimal 时请写string类型 防止精度丢失
        remitBatchRequestDTO.setBatchAmt(new BigDecimal(orderAmt));//批次总金额

        remitBatchRequestDTO.setServerCallbackUrl("http://www.poshb.cn:8081/noauth/payment/callBack");//打款结果异步通知地址

        List<RemitDetailRequestDTO> list = new ArrayList<RemitDetailRequestDTO>();
        RemitDetailRequestDTO detail_1 = new RemitDetailRequestDTO();
        detail_1.setCustNo(appId);//商户应用ID
        // String cust_1 = UUID.randomUUID().toString().replace("-","");
        String orderNo = OrderUtil.randomOrderNo();
        System.out.println("cust_1  :  " + orderNo);
        detail_1.setCustOrderNo(orderNo);//商户订单号
        //new BigDecimal 时请写string类型 防止精度丢失
        detail_1.setOrderAmt(new BigDecimal(orderAmt));//打款金额
        detail_1.setRecvCustName(recvCustName);//收款者姓名
        detail_1.setRecvMobile(recvMobile);//收款者手机号
        detail_1.setRecvIdType(recvIdType);//收款者证件类型
        detail_1.setRecvIdNo(recvIdNo);//收款整证件号
        detail_1.setRecvCardNo(recvCardNo);//收款者银行卡号
        detail_1.setRecvBankName(recvBankName);//收款银行
        list.add(detail_1);

//        RemitDetailRequestDTO detail_2 = new RemitDetailRequestDTO();
//        detail_2.setCustNo(appId);
//        String cust_2 = UUID.randomUUID().toString().replace("-","");
//        System.out.println("cust_2  :  "+cust_2);
//        detail_2.setCustOrderNo(cust_2);
//        //new BigDecimal 时请写string类型 防止精度丢失
//        detail_2.setOrderAmt(new BigDecimal("60"));
//        detail_2.setRecvCustName("");
//        detail_2.setRecvMobile("");
//        detail_2.setRecvIdType("");
//        detail_2.setRecvIdNo("");
//        detail_2.setRecvCardNo("");
//        detail_2.setRecvBankName("");
//      setRecvBankName  list.add(detail_2);

        remitBatchRequestDTO.setRemitDetailList(list);

        BaseResponse responseDTO = secClient.excute(remitBatchRequestDTO);
        System.out.println("---------------支付申请时间：" + str + "------------------");
        System.out.println(responseDTO.toString());
        System.out.println(responseDTO.getResponse());

        if (responseDTO.getCode().equals("0000")) {
            // 成功
            AjaxResult.success("余额预支付成功");
            System.out.println("余额预支付成功");
        } else if (responseDTO.getCode().equals("0001")) {
            AjaxResult.error("余额预支付失败-参数为空或格式错误");
            System.out.println("余额预支付失败-参数为空或格式错误");
        } else if (responseDTO.getCode().equals("9999")) {
            AjaxResult.error("余额预支付失败-系统异常");
            System.out.println("余额预支付失败-系统异常");
        } else if (responseDTO.getCode().equals("9001")) {
            AjaxResult.error("余额预支付失败-账户不存");
            System.out.println("余额预支付失败-账户不存");
        } else if (responseDTO.getCode().equals("9002")) {
            AjaxResult.error("余额预支付失败-账户余额不足");
            System.out.println("余额预支付失败-账户余额不足");
        } else if (responseDTO.getCode().equals("5003")) {
            AjaxResult.error("余额预支付失败-超过限额");
            System.out.println("余额预支付失败-超过限额");
        } else if (responseDTO.getCode().equals("0010")) {
            AjaxResult.error("余额预支付失败-客户状态非法");
            System.out.println("余额预支付失败-客户状态非法");
        }
    }

    /*  public static void main(String[] args)  throws Exception{
        Map<String ,String> params = new HashMap();
        params.put("app_id","YX0000000000001"); //可以放入请求头中
        params.put("method","settle.remit.api.payment");
        params.put("sign_type","RSA2");
        params.put("timestamp","2013-01-01 08:08:08"); //毫秒
        params.put("version","1.0");
        params.put("merchant_request_no","10000000000");
        TestDTO testDTO = new TestDTO();
        testDTO.setMany("10086");
        testDTO.setOrderNo("这是个订单号");

        List<EntyDTO> list = new ArrayList<>();
        EntyDTO entyDTO = new EntyDTO();
        entyDTO.setTest1("1");
        entyDTO.setTest2("2");
        list.add(entyDTO);
        testDTO.setList(list);
        params.put("biz_content", JsonUtils.ObjectTojson(testDTO));//JSON 可加密
        //参数进行RES签名
        String signa = SecSignature.rsaSign(params, merPrivate,"UTF-8","RSA2");

        System.out.println(signa);

        params.put("sign",signa);

        String resp = WebUtils.doPost("http://127.0.0.1:8003/secopenplatfrom/sec/gateway",params,"UTF-8",100000,100000,null,0);

        System.out.println(resp);
    }*/
}
